package com.qingshanhui.home_module.module2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qingshanhui.home_module.R;
import com.qingshanhui.home_module.utils.DisplayUtils;
import com.qingshanhui.home_module.utils.GridItemDecoration;
import com.qingshanhui.home_module.utils.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Desc:顶部Banner控件
 * <p>
 * Author: AnYaBo
 * Date: 2021/1/31
 * Copyright: Copyright (c) 2016-2022
 * Company: @小牛科技
 * Email:anyabo@xiaoniu.com
 * Update Comments:
 */
public class RecommendViewHolder implements Banner.BannerViewHolder<Object> {
    private int rowCount;

    public RecommendViewHolder(int rowCount) {
        this.rowCount = rowCount;
    }

    @Override
    public View createView(Context context, int position, Object data) {
        RecyclerView recyclerView = new RecyclerView(context);
        int margin = (int) DisplayUtils.dip2px(context,12);
        recyclerView.setPadding(margin,0,margin,0);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        GridLayoutManager layoutManager = new GridLayoutManager(context, rowCount == 0 ? 2 : rowCount, LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        RecommendAdapter recommendAdapter = new RecommendAdapter((List<RecommendModel>) data);
        recyclerView.setAdapter(recommendAdapter);
        float space = DisplayUtils.dip2px(context,5);
        GridItemDecoration divider = new GridItemDecoration.Builder(context)
                .setHorizontalSpan(space)
                .setVerticalSpan(space)
                .setShowLastLine(true)
                .build();
        recyclerView.addItemDecoration(divider);
        return recyclerView;
    }

    static class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.MyViewHolder>{

        private List<RecommendModel> list = new ArrayList<>();

        public RecommendAdapter(List<RecommendModel> list){
            this.list = list;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_item, parent,
                    false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position){
            Context context = holder.itemView.getContext();
            RecommendModel recommendModel = list.get(position);
            Glide.with(context).load(recommendModel.logo).into(holder.logoIv);
            holder.titleTv.setText(recommendModel.title);
            holder.startDateTv.setText(recommendModel.startDate);
            holder.addressTv.setText(recommendModel.address);
        }

        @Override
        public int getItemCount(){
            return list.size();
        }

        static class MyViewHolder extends RecyclerView.ViewHolder{
            ImageView logoIv;
            TextView titleTv;
            TextView startDateTv;
            TextView addressTv;

             MyViewHolder(View view) {
                 super(view);
                 logoIv = view.findViewById(R.id.recommend_logo_iv);
                 titleTv = view.findViewById(R.id.recommend_title_tv);
                 startDateTv = view.findViewById(R.id.recommend_start_date_tv);
                 addressTv = view.findViewById(R.id.recommend_address_tv);
             }
        }
    }

}
